﻿using System.Collections.Generic;
using System.Data;
using DataAccess.Implementations;
using NH.Domain.Implementations;
using NH.Domain.Interfaces;
using NH.Provider.Interfaces;

namespace NH.Provider.Implementations
{
    public class DoctorHandler : IDoctorHandler
    {
        private readonly SqlHelper _sqlHelper;

        public DoctorHandler()
        {
            _sqlHelper = new SqlHelper();
        }

        public IList<IDoctor> GetDoctors(int? departmentId)
        {
            var doctors = new List<IDoctor>();

            try
            {
                _sqlHelper.OpenConnection();
                _sqlHelper.CreateCommand("dbo.GetDoctorsList", CommandType.StoredProcedure);
                _sqlHelper.AddParameter("@DepartmentID", SqlDbType.Int, ParameterDirection.Input, departmentId);

                var sqlReader = _sqlHelper.ExecuteReader(CommandBehavior.CloseConnection);
                while (sqlReader.Read())
                {
                    doctors.Add(new Doctor
                                    {
                                        Id = SqlUtil.SetValue(sqlReader["ID"], 0),
                                        FirstName = SqlUtil.SetValue(sqlReader["FirstName"], string.Empty),
                                        MiddleName = SqlUtil.SetValue(sqlReader["MiddleName"], string.Empty),
                                        LastName = SqlUtil.SetValue(sqlReader["LastName"], string.Empty)
                                    });
                }
            }
            finally
            {
                _sqlHelper.CloseConnection();
            }

            return doctors;
        }
    }
}
